package com.acme.bankapp.service.bank;

import com.acme.bankapp.domain.bank.Bank;
import com.acme.bankapp.domain.bank.Client;
import com.acme.exceptions.ClientDuplicateException;
import com.acme.exceptions.ClientNotExistException;
import com.acme.exceptions.NotEnoughFundsException;

public class BankService {
	
	public static void addClient(Bank bank, Client client) throws ClientDuplicateException, ClientNotExistException {
		for (int i = 0; i < bank.getClientCount(); i++) {
			if(client.getName() == bank.getClientById(i).getName()) {
				throw new ClientDuplicateException("Client already exist!");
			}
		}
		bank.addClient(client);
	}
	
	public static void modifyBank(Bank bank, int clientId, double changeAmount) throws NotEnoughFundsException, ClientNotExistException {
		if(changeAmount >= 0) {
			bank.getClientById(clientId).getAccount().deposit(changeAmount);
		} else {
			bank.getClientById(clientId).getAccount().withdraw(-changeAmount);
		}
	}
	
	public static Client getClientById(Bank bank, int id) throws ClientNotExistException {
		return bank.getClientById(id);
	}
	
	public static void printBalance(Bank bank) throws ClientNotExistException {
		for(int i = 0; i < bank.getClientCount(); i++) {
			System.out.println("Client " + i + " balance: " + bank.getClientById(i).getAccount().getBalance());
		}
	}
	
	public static void printMaximumAmountToWithdraw(Bank bank) throws ClientNotExistException {
		for(int i = 0; i < bank.getClientCount(); i++) {
			if (bank.getClientById(i).getAccount().getAccountType(bank.getClientById(i).getAccount()) == "SavingAccount") {
				System.out.println("Client " + i + " Max amount to withdraw: " + bank.getClientById(i).getAccount().maximumAmountToWithdraw());
			} else {
				System.out.println("Client " + i + " Max amount to withdraw: " + bank.getClientById(i).getAccount().maximumAmountToWithdraw());
			}
		}
	}
}
